home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / os2 / cenv2_19.arj / CMM_VS_C.DOC < prev    next >
Text File  |  1994-03-09  |  17KB  |  401 lines

  1.                      CEnvi Shareware Manual, Chapter 3:
  2.                       Cmm versus C, for C Programmers
  3.  
  4.                      CEnvi unregistered version 1.009
  5.                                9 March 1994
  6.  
  7.                        CEnvi Shareware User's Manual
  8.  
  9.           Copyright 1993, Nombas, All Rights Reserved.
  10.           Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
  11.           (617)391-6595
  12.  
  13.           Thank you for trying this shareware version of CEnvi from Nombas,
  14.           a member of the Association of Shareware Professionals (ASP).
  15.  
  16. 3.  Cmm versus C: The Cmm language for C programmers
  17.  
  18.           This chapter is for those who already know how to program in the
  19.           C language.  This chapter describes only those elements of Cmm
  20.           that differ from standard C, and so if you don't already
  21.           understand C, then this shouldn't have much meaning for you.
  22.           Non-C programmers should instead look at the previous chapter.
  23.  
  24.           Since it is assumed that readers of this chapter are already
  25.           knowledgeable in C, only those aspects of Cmm that differ from C
  26.           are described here.  If it's not mentioned here, then assume that
  27.           Cmm behavior will be standard C.
  28.  
  29.           Deviations from C are a result of these two harmonious Cmm
  30.           directives: Convenience and Safety.  Cmm is different from C
  31.           where the change makes Cmm more convenient for small programs,
  32.           command-line code, or scripting files, or if unaltered C rules
  33.           encourage coding that is potentially unsafe.
  34.  
  35. 3.1.  C Minus Minus
  36.  
  37.           Cmm is "C minus minus" where the minuses are Type Declarations
  38.           and Pointers.  If you already know C and can remember to forget
  39.           these two aspects of C (I repeat, no Type Declarations and no
  40.           Pointers) then you know Cmm.  If you were to take C code, and
  41.           delete all the lines, code-words, and symbols that either declare
  42.           data types or explicitly point to data, then you would be left
  43.           with Cmm code; and although you would be removing bytes of source
  44.           code, you would not be removing capabilities.
  45.  
  46.           All of the details below that compare Cmm against C follow from
  47.           the general rule:
  48.             *Cmm is C minus Type Declarations and minus Pointers.
  49.  
  50. 3.2.  Data Types
  51.  
  52.           The only recognized data types are Float, Long, and Byte.  The
  53.           words "Float", "Long", and "Byte" do not appear in Cmm source
  54.           code; instead, the data types are determined by context.  For
  55.           instance 6 is a Long, 6.6 is a Float, and '6' is a Byte.  Byte is
  56.           unsigned, and the other types are signed.
  57.  
  58. 3.3.  Automatic Type Declaration
  59.  
  60.           There are no type declarators and no type casting.  Types are
  61.           determined from context.  If the code says "i=6" then i is a
  62.           Long, unless a previous statement has indicated otherwise.
  63.  
  64.           For instance, this C code:
  65.               int Max(int a,int b)
  66.               {
  67.                 int result;
  68.                 result = ( a < b ) ? b : a ;
  69.                 return result;
  70.               }
  71.           could become this Cmm code:
  72.               Max(a,b)
  73.               {
  74.                 result = ( a < b ) ? b : a ;
  75.                 return result;
  76.               }
  77.  
  78. 3.4.  Array Representation
  79.  
  80.           Arrays are used in Cmm much like they are in C, except that they
  81.           are stored differently: a first-order array (e.g., a string) is
  82.           stored in consecutive bytes in memory, but arrays of arrays are
  83.           not in consecutive memory locations.  The C declaration "char
  84.           c[3][3];" would state that there are nine consecutive bytes in
  85.           memory.  In Cmm a similar statement such as "c[2][2] = 'A'" would
  86.           tell you that there are (at least) three arrays of characters,
  87.           and the third array of arrays has (at least) three characters in
  88.           it; and although the characters in c[0] are in consecutive bytes,
  89.           and the characters in c[1] are in consecutive bytes, the two
  90.           arrays c[0] and c[1] are not necessarily adjacent in memory.
  91.  
  92. 3.4.1   Array Arithmetic
  93.  
  94.           When one array is assigned to the other, as in:
  95.               foo = "cat";
  96.               goo = foo;
  97.           then both variables define the same array and start at the same
  98.           offset 0.  In this case, if foo[2] is changed then you will find
  99.           that goo[2] has also been changed.
  100.  
  101.           Integer addition and subtraction can also be performed on arrays.
  102.           Array addition or subtraction sets where the array is based.  By
  103.           altering the previous code segment to:
  104.               foo = "cat";
  105.               goo = foo + 1;
  106.           goo and foo would now be arrays containing the same data, except
  107.           that now goo is based one element further, and foo[2] is now the
  108.           same data as goo[1].
  109.  
  110.           To demonstrate:
  111.               foo = "cat";  // foo[0] is 'c', foo[1] = 'a'
  112.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  113.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  114.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  115.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  116.  
  117. 3.4.2   Automatic Array Allocation
  118.  
  119.           Arrays are dynamic, and any index, (positive or negative) into an
  120.           array is always valid.  If an element of an array is referred to,
  121.           then the Cmm must see to it that such an element will exist.  For
  122.           instance if the first statement in the Cmm source code is "foo[4]
  123.           = 7;" then the Cmm interpreter will make an array of 5 integers
  124.           referred to by the variable foo.  If a statement further on
  125.           refers to "foo[6]" then the Cmm interpreter will grow foo, if it
  126.           has to, to ensure that the element foo[6] exists.  This works
  127.           with negative indices as well.  When you refer to foo[-10], then
  128.           foo is grown in the other direction if it needs to be, but foo[4]
  129.           will still refer to that "7" you put there earlier.  Arrays can
  130.           reach any dimension order, so that foo[6][7][34][-1][4] is a
  131.           valid value.
  132.  
  133. 3.5.  Structures
  134.  
  135.           Structures are created dynamically, and their elements are not
  136.           necessarily contiguous in memory.  When CEnvi comes across the
  137.           statement 'foo.animal = "dog"' it creates a structure element of
  138.           foo that is referred to as "animal" and is an array of
  139.           characters, and this "animal" variable is thereafter carried
  140.           around with "foo" (much like a stem variable in REXX).  The
  141.           resulting code looks very much like regular C code, except that
  142.           there is not a separate structure definition anywhere.
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.           This C code:
  153.  
  154.               struct Point {
  155.                 int Row;
  156.                 int Column;
  157.               };
  158.  
  159.               struct Square {
  160.                 struct Point BottomLeft;
  161.                 struct Point TopRight;
  162.               };
  163.  
  164.               void main()
  165.               {
  166.                 struct Square sq;
  167.                 int Area;
  168.                 sq.BottomLeft.Row = 1;
  169.                 sq.BottomLeft.Column = 15;
  170.                 sq.TopRight.Row = 82;
  171.                 sq.TopRight.Column = 120;
  172.                 Area = AreaOfASquare(sq);
  173.               }
  174.  
  175.               int AreaOfASquare(struct Square s)
  176.               {
  177.                 int width, height;
  178.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  179.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  180.                 return( length * height );
  181.               }
  182.  
  183.           can be changed into the equivalent Cmm code simply be removing
  184.           declaration lines, resulting in:
  185.  
  186.               main()
  187.               {
  188.                 sq.BottomLeft.Row = 1;
  189.                 sq.BottomLeft.Column = 15;
  190.                 sq.TopRight.Row = 82;
  191.                 sq.TopRight.Column = 120;
  192.                 Area = AreaOfASquare(sq);
  193.               }
  194.  
  195.               int AreaOfASquare(s)
  196.               {
  197.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  198.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  199.                 return( length * height );
  200.               }
  201.  
  202.           Structures can be passed, returned, and modified just as any
  203.           other variable.  Of course structures and arrays are independent,
  204.           so you could very well have the statement "foo[8].animal.forge[3]
  205.           = bil.bo".
  206.  
  207. 3.6.  Passing Variables by Reference
  208.  
  209.           By default, LValues in Cmm are passed to functions by reference,
  210.           and so if the function alters a variable then the variable in the
  211.           calling function is altered as well IF IT IS AN LVALUE.  So
  212.           instead of this C code:
  213.  
  214.               main() {
  215.                 .
  216.                 .
  217.                 .
  218.                 CQuadrupleInPlace(&i);
  219.                 .
  220.                 .
  221.                 .
  222.               }
  223.  
  224.               void CQuadrupleInPlace(int *j)
  225.               {
  226.                 *j *= 4;
  227.               }
  228.  
  229.           the Cmm version would be:
  230.  
  231.               main() {
  232.                 .
  233.                 .
  234.                 .
  235.                 QuadrupleInPlace(i);
  236.                 .
  237.                 .
  238.                 .
  239.               }
  240.  
  241.               void QuadrupleInPlace((j)
  242.               {
  243.                 j *= 4;
  244.               }
  245.  
  246.           If the rare circumstance arises that you want to pass a copy of
  247.           an LValue to a function, instead of passing the variable by
  248.           reference, then you can use the Cmm "copy-of" operator "=".
  249.           foo(=i) can be interpreted as saying "pass a value equal to i,
  250.           but not i itself"; so that "foo(=i) ... foo(j) { j *= 4; }" would
  251.           not change the value at i.
  252.  
  253.           Note that for this Cmm version, the following calls to
  254.           QuadrupleInPlace() would be valid, but no value will have changed
  255.           upon return from QuadrupleInPlace() because an LValue is not
  256.           being passed:
  257.               QuadrupleInPlace(8);
  258.               QuadrupleInPlace(i+1);
  259.               QuadrupleInPlace(=1);
  260.  
  261. 3.7.  Data Pointers(*) and Addresses(&)
  262.  
  263.           No pointers.  None.  The "*" symbol NEVER means "pointer" and the
  264.           "&" symbol never means "address".  This may at first cause
  265.           seasoned C programmers to gasp in disbelief, but it turns out to
  266.           be not such a big deal, and these two operators are seldom
  267.           missed, after considering these two rules:
  268.               1) "*var" can be replaced in all instances by "var[0]"
  269.               2) variables (if LValues) are passed by reference
  270.  
  271. 3.8.  Case Statements
  272.  
  273.           Case statements in a switch statement may be a constant, a
  274.           variable, or any other statement that can be evaluated to a
  275.           variable.  So you might see this Cmm code:
  276.  
  277.               switch(i) {
  278.                 case 4:
  279.                 case foe():
  280.                 case sqrt(foe()):
  281.                 case (PILLBOX * 3 - 2):
  282.                 default:
  283.               }
  284.  
  285. 3.9.  Initialization: Code external to functions
  286.  
  287.           All code that is not inside any function block is interpreted
  288.           before main() is called.  So the following Cmm code:
  289.  
  290.               printf("hey there ");
  291.  
  292.               main()
  293.               {
  294.                 printf("ho there ");
  295.               }
  296.  
  297.               printf("hi there ");
  298.  
  299.           would result in the output "hey there hi there ho there ".
  300.  
  301. 3.10. Unnecessary tokens
  302.  
  303.           If symbols are redundant, then they are usually unnecessary in
  304.           Cmm.  This allows for more flexibility in the non-C-trained and
  305.           also lets more code get in the tiny space available on the
  306.           command line.  Besides, I got tired of my compiler saying
  307.           "missing semi-colon"; What good is a semi-colon if it doesn't
  308.           tell the compiler anything new?  So you can have the statement
  309.           "foo()" as well as "foo();".  It certainly doesn't hurt to have
  310.           the semi-colon there, especially when it can clarify a "return;"
  311.           statement, for example, but it isn't necessary.  Similarly, "("
  312.           and ")" are often unnecessary, so you may have "while a < b a++"
  313.           as a complete statement.
  314.  
  315. 3.11. #include
  316.  
  317.           The #include statement has been enhanced for reading source
  318.           snippets from within other types of files.  So we have
  319.  
  320.               #Include <filespec,Extension,Prefix,HeaderLine,FooterLine>
  321.  
  322.           where filespec is the same as in C's #include <filespec>
  323.           statement, Extension is a file extension (such as BAT) that may
  324.           be added to the filespec (so batch files can say #include
  325.           <%0,bat>", Prefix specifies that only those lines in filespec
  326.           that begin with Prefix will be source, and HeaderLine and
  327.           FooterLine specify that source will be read only from sections of
  328.           filespec between HeaderLine and FooterLine.  If a full path is
  329.           not specified then CEnvi searches for the file in various paths
  330.           in this order:
  331.             *Search the current directory.
  332.             *If the code is run from a *.cmm source file, then search in
  333.               the source directory for the *.cmm file.
  334.             *If this is the Windows version of CEnvi, searches all the
  335.               files in the CMMPATH profile value (from WIN.INI in the
  336.               [CEnvi] section).
  337.             *Search all directories in the CMMPATH environment variable.
  338.             *Search the directory that CEnvi.exe is executed from.
  339.             *Search all directories from the PATH environment variable.
  340.  
  341.           In CEnvi a file will not be included more than once, and so if it
  342.           has already been included, a second (or third) #include statement
  343.           will have no effect.
  344.  
  345. 3.12. Macros
  346.  
  347.           Function macros are not supported.  Since speed is not of primary
  348.           importance, a macro gains little over a function call, and so any
  349.           macros may simply become functions.
  350.  
  351.           Token replacement macros ("#define NULL 0") are supported in Cmm.
  352.  
  353. 3.13. Back-quote strings
  354.  
  355.           The back-quote character (`), also known as a "back-tick" or
  356.           "grave accent", can be used in Cmm in place of a double quote (")
  357.           to specify a string where escape sequences are not to be
  358.           translated.  So, for example, here are two ways to describe the
  359.           same file name:
  360.               "c:\\autoexec.bat"  // traditional method
  361.               `c:\autoexec.bat`   // alternative method
  362.  
  363. 3.14. Converting existing C code to Cmm
  364.  
  365.           Converting existing C code to Cmm, should you choose to do so, is
  366.           mostly a process of deleting unnecessary text.  You search on
  367.           type declarations, such as "int", "float", "struct", "char",
  368.           "[]", etc... and then delete these declaration strings.  For
  369.           instance, these instances of C code (or C++ code) on the left can
  370.           be replaced by the Cmm code on the right:
  371.  
  372.                    C                               Cmm     
  373.               ----------                      -------------
  374.  
  375.               int i;    ................... i (or nothing at all)
  376.  
  377.               int foo = 3; ................ foo = 3;
  378.  
  379.               struct {    ................... /* nothing */
  380.                 int row;
  381.                 int col;
  382.               }
  383.  
  384.               char name[] = "George"; ..... name = "George";
  385.  
  386.               int go(int a,char *s,int &c)    go(a,s,c)
  387.               int zoo[] = { 1, 2, 3 }; .... zoo = { 1, 2, 3 };
  388.  
  389.           The next step in converting C to Cmm is to search for the address
  390.           and pointer operators ("*", "&").  If the '&' and '*' are
  391.           together so that the address of a variable is passed to a
  392.           function, then both of these operators become unnecessary because
  393.           Cmm passes lvars by reference.  If there are still "*" found then
  394.           they are usually referring to the zeroth value of a pointer
  395.           address, and so can be replaced with [0], as in "*foo = 4"
  396.           replaced by "foo[0] = 4".  Finally, the "->" operator for
  397.           structures must be replaced by "." either because the structure
  398.           is now being passed by referenced or because the element of the
  399.           structure is being referred to by its array index: "foo->row" may
  400.           need to become "foo[0].row".
  401.